package com.gali.juc;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther: gali
 * @Date: 2022-08-24 20:48
 * @Description:
 */
class myData{
    ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(()->0);

    public void add(){
        threadLocal.set(1+threadLocal.get());
    }
}
public class ThreadLocalToExecutors {
    public static void main(String[] args) {
        myData myData = new myData();
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        try {
            try {
                for (int i = 0; i < 10; i++) {
                    threadPool.submit(()->{
                        Integer integer = myData.threadLocal.get();
                        myData.add();
                        Integer integer1 = myData.threadLocal.get();
                        System.out.println(Thread.currentThread().getName()+"\t "+integer+"\t"+integer1);
                    });
                }
            } finally {
                myData.threadLocal.remove();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
